package com.zztl.infiniteBand.presenter

import android.content.Context
import android.net.Uri
import com.zztl.infiniteBand.base.BasePresenter
import com.zztl.infiniteBand.model.NoDataResponseBean
import com.zztl.infiniteBand.model.PersonInfoBean
import com.zztl.infiniteBand.model.UploadFileBean
import com.zztl.infiniteBand.net.FilterSubscriber
import com.zztl.infiniteBand.net.RetorfitServiceManger
import com.zztl.infiniteBand.net.converter.DataConverter
import com.zztl.infiniteBand.net.imp.RequestKeys
import com.zztl.infiniteBand.ui.view.mvpview.PersonInfoView
import java.io.File
import java.net.URI
import java.net.URISyntaxException
import java.util.*

/**
 * @desc
 * @auth ${user}
 * @time 2018/6/13  19:03
 */
class PersenInfoPresenter : BasePresenter<PersonInfoView>() {
    fun getUserInfo(context: Context) {
        addSubscribe(RetorfitServiceManger
                .observe(RetorfitServiceManger.instance!!.apiService
                .getInfo("")).subscribe(object : FilterSubscriber<PersonInfoBean>(context) {
            override fun success(infoBean: PersonInfoBean) {
                view!!.getInfoSuccess(infoBean)
            }


        }))
    }

    fun updateMyPhoto(uri: Uri?, context: Context) {
        if (uri != null) {
            var file: File? = null
            try {
                file = File(URI(uri.toString()))
            } catch (e: URISyntaxException) {
                e.printStackTrace()
            }

            if (null != file) {
                val path = file.absolutePath
                addSubscribe(RetorfitServiceManger
                        .observe(RetorfitServiceManger.instance!!.apiService
                                .uploadImage(RetorfitServiceManger.parseImageRequestBody(RequestKeys.PHOTO, file),
                                        RetorfitServiceManger.parseRequestBody("icon")))
                        .subscribe(object : FilterSubscriber<UploadFileBean>(context) {
                            override fun success(bean: UploadFileBean) {
                                view!!.loadSuccess(bean)
                            }

                            override fun fail() {
                                super.fail()
                                view!!.loadFail()
                            }
                        }))
            }
        }
    }

    fun updatePersonInfo(key: String, value: String, context: Context) {
        val params = HashMap<String, String>()
        params[key] = value
        addSubscribe(RetorfitServiceManger
                .observe(RetorfitServiceManger.instance!!.apiService
                .setPersonInfo(DataConverter.instance.toJson(params)))
                .subscribe(object : FilterSubscriber<NoDataResponseBean>(context) {
                    override fun success(bean: NoDataResponseBean) {
                        view!!.upDateSuccess(true)
                    }

                    override fun fail() {
                        super.fail()
                        view!!.upDateSuccess(false)
                    }
                }))
    }
}
